/*
* $Id: ChangeInspectView.cpp 79 2010-04-05 21:35:04Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeInspectView.h"
#include "ViewFieldValuesHelper.h"
#include "Database/Tables/JournalTable.h"
#include "Database/Operation/GetInspectOperation.h"
#include "Database/Operation/ChangeInspectOperation.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Visual/Drawers/EditorDrawer/SectionItem.h"
#include "Settings/Journal/RecordViewSettings.h"
#include "Settings/SettingControlIds.h"
#include "Settings/DinIntDefined.h"

#define TITLE_ID		1
#define DATE_INSPECT_ID			2
#define FRAME_INSPECT_ID		3
#define STRENGTH_INSPECT_ID		4
#define EBQ_INSPECT_ID			5
#define WAX_INSPECT_ID			6
#define AMBROSIA_INSPECT_ID		7
#define PUMP_INSPECT_ID			8
#define FEEDING_INSPECT_ID		9
#define DISEASES_INSPECT_ID		10
#define NOTE_INSPECT_ID			11
#define USER_FIELD_ID			12
#define HOUSING_FIELD_ID(i, j)	(i<<8)|j
#define HONEY_FIELD_ID			1
#define BROOD_FIELD_ID			2
#define OPEN_BROOD_FIELD_ID		3
#define CLOSE_BROOD_FIELD_ID	4
#define GOOGS_HONEY_FIELD_ID	5

CChangeInspectView::CChangeInspectView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CChangeInspectView::~CChangeInspectView(){}

void CChangeInspectView::Init(){
	m_editorDrawer.Clear();
	m_aHousing.clear();
	m_editorDrawer.AddItem(new CHeaderItem(IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_INSPECT_ID), TITLE_ID));

	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CJournalTable journal;
	journal.m_content.s_nId = m_nIdParameter;
	IResult* pResult = journal.GetValue(pDb);
	int nIdRecord = 0;
	time_t nDate = 0;
	int nFrame = 0, nStrength = 0, nEBQ = 0, nWax = 0;
	int nAmbrosia = 0, nPumpHoney = 0, nFeeding = 0, nDisease = 0;
	std::tstring strNote;
	if(pResult->GetSizeResult()){
		CJournalTable::content* pContent = (CJournalTable::content*)(*pResult)[0];
		nDate = pContent->s_date;
		nFrame = pContent->s_nFrame;
		nStrength = pContent->s_nStrength;
		nEBQ = pContent->s_QueenBee;
		nWax = pContent->s_nWax;
		nAmbrosia = pContent->s_nAmbrosia;
		nPumpHoney = pContent->s_nEvacuation;
		nFeeding = pContent->s_nFeeding;
		nDisease = pContent->s_disease;
		nIdRecord = pContent->s_nIdRecord;
		strNote = pContent->s_strNote;
	}
	pResult->Free();
	CGetInspectOperation operation;
	operation.m_content.s_nIdJournal = m_nIdParameter;
	pResult = operation.GetValue(pDb);
	std::vector<CGetInspectOperation::content> aHousings;
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CGetInspectOperation::content* pContent = (CGetInspectOperation::content*)(*pResult)[i];
		aHousings.push_back(*pContent);
		m_aHousing.push_back(pContent->s_nIdHousing);
	}
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);

	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_DATE_ID), DATE_INSPECT_ID, editItem::DATE, CConvertFunction::ConvertFromDate(nDate)));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FRAME_ID), FRAME_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(nFrame)));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_BEES_ID), STRENGTH_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(nStrength)));
	CComboBoxItem* pComboItem = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_EBQ_ID), EBQ_INSPECT_ID);
	pComboItem->AddValueItem(CJournalTable::content::DEFAULT, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_DEFAULT_ID));
	pComboItem->AddValueItem(CJournalTable::content::LOOK, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_LOOK_ID));
	pComboItem->AddValueItem(CJournalTable::content::ABSENCE, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_ABSENCE_ID));
	pComboItem->AddValueItem(CJournalTable::content::FETAL, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_FETAL_ID));
	pComboItem->SetValue(nEBQ);
	m_editorDrawer.AddItem(pComboItem);
	CRecordViewSettings jSettings;
	CEditItem* pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_WAX_ID), WAX_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(nWax));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_GIVE_WOX_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_AMBROSIA_ID), AMBROSIA_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(nAmbrosia));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_AMBROSIA_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_PUMP_HONEY_ID), PUMP_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(nPumpHoney));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_PUMP_HONEY_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FEEDING_ID), FEEDING_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(nFeeding));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_FEEDING_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pComboItem = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_DISEASES_ID), DISEASES_INSPECT_ID);
	for(int i = 0, id = DISEASE_HEALTHY_ID; i < 33; i++, id++)
		pComboItem->AddValueItem(i, IApplication::GetApp()->GetLanguageManager()->GetString(id));
	pComboItem->SetValue(nDisease);
	m_editorDrawer.AddItem(pComboItem);
	if(jSettings.GetCurrentValue(CHECK_DISEASES_ID) != CHECK_BOX_STATE_1)
		pComboItem->Hide(true);
	pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_INSPECT_ID, editItem::TEXT, strNote);
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);

	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(CViewFieldValuesHelper::INSPECT, USER_FIELD_ID, nIdRecord);

	for(std::vector<CGetInspectOperation::content>::iterator it = aHousings.begin(); it != aHousings.end();it++){
		m_editorDrawer.AddItem(new CSectionItem(IApplication::GetApp()->GetLanguageManager()->GetString(HOUSING_ID) + _T(" ") + it->s_strNameHousing, HOUSING_FIELD_ID(it->s_nIdHousing, 0)));
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_BROOD_ID), HOUSING_FIELD_ID(it->s_nIdHousing, BROOD_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->s_nBrood)));
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_HONEY_ID), HOUSING_FIELD_ID(it->s_nIdHousing, HONEY_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->s_nHoney)));
		pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_OPEN_BROOD_ID), HOUSING_FIELD_ID(it->s_nIdHousing, OPEN_BROOD_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->s_nOpenBrood));
		m_editorDrawer.AddItem(pItem);
		if(jSettings.GetCurrentValue(CHECK_OPEN_BROOD_ID) != CHECK_BOX_STATE_1)
			pItem->Hide(true);
		pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_CLOSE_BROOD_ID), HOUSING_FIELD_ID(it->s_nIdHousing, CLOSE_BROOD_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->s_nCloseBrood));
		m_editorDrawer.AddItem(pItem);
		if(jSettings.GetCurrentValue(CHECK_SEAL_BROOD_ID) != CHECK_BOX_STATE_1)
			pItem->Hide(true);
		pItem = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_GOODS_HONEY_ID), HOUSING_FIELD_ID(it->s_nIdHousing, GOOGS_HONEY_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->s_nGoodsHoney));
		m_editorDrawer.AddItem(pItem);
		if(jSettings.GetCurrentValue(CHECK_GOODS_HONEY_ID) != CHECK_BOX_STATE_1)
			pItem->Hide(true);
	}
}

void CChangeInspectView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_CHANGE_INSPECT;
}
bool CChangeInspectView::IsViewState(State state){
	return state.m_type == JournalEnum::CHANGE_INSPECT;
}

BEGIN_MESSAGE_MAP(CChangeInspectView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CChangeInspectView::OnSavePoint(){
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CChangeInspectOperation operation;
	operation.m_content.s_nId = m_nIdParameter;
	operation.m_content.s_date = CConvertFunction::ConvertFromDate(m_editorDrawer.GetValueItem(DATE_INSPECT_ID));
	operation.m_content.s_nFrame = _ttoi(m_editorDrawer.GetValueItem(FRAME_INSPECT_ID).c_str());
	operation.m_content.s_nStrength = _ttoi(m_editorDrawer.GetValueItem(STRENGTH_INSPECT_ID).c_str());
	operation.m_content.s_beeQueenStatus = _ttoi(m_editorDrawer.GetValueItem(EBQ_INSPECT_ID).c_str());
	operation.m_content.s_nWax = _ttoi(m_editorDrawer.GetValueItem(WAX_INSPECT_ID).c_str());
	operation.m_content.s_nAmbrosia = _ttoi(m_editorDrawer.GetValueItem(AMBROSIA_INSPECT_ID).c_str());
	operation.m_content.s_nPumpHoney = _ttoi(m_editorDrawer.GetValueItem(PUMP_INSPECT_ID).c_str());
	operation.m_content.s_nFeeding = _ttoi(m_editorDrawer.GetValueItem(FEEDING_INSPECT_ID).c_str());
	operation.m_content.s_disease = _ttoi(m_editorDrawer.GetValueItem(DISEASES_INSPECT_ID).c_str());
	operation.m_content.s_strNote = m_editorDrawer.GetValueItem(NOTE_INSPECT_ID);
	for(int i = 1; i <= m_nUserFields; i++)
		operation.m_content.s_aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	for(std::vector<int>::iterator it = m_aHousing.begin(); it != m_aHousing.end(); it++){
		int nIdHousing = *it;
		int nBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(*it, BROOD_FIELD_ID)).c_str());
		int nHoney = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(*it, HONEY_FIELD_ID)).c_str());
		int nOpenBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(*it, OPEN_BROOD_FIELD_ID)).c_str());
		int nCloseBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(*it, CLOSE_BROOD_FIELD_ID)).c_str());
		int nGoodsHoney = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(*it, GOOGS_HONEY_FIELD_ID)).c_str());
		operation.m_content.s_aInspects.push_back(CChangeInspectOperation::inspect(nIdHousing, nBrood, nHoney, nOpenBrood, nCloseBrood, nGoodsHoney));
	}
	operation.UpdateValue(pDb);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	__super::OnCancel();
}